package com.example.jetpackcomposebasic

import androidx.compose.foundation.layout.BoxWithConstraints
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester.Companion.createRefs
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.ConstraintSet
import androidx.constraintlayout.compose.Dimension

/**
 * 需要添加 constraintlayout-compose 依赖
 * */
@Preview
@Composable
fun PreviewConstraintLayout() {

    ConstraintLayout {
        // ConstraintLayout 中的每一个元素都需要关联一个引用
        val (button, text) = createRefs()

        Button(
            onClick = { /*TODO*/ },
            // 提供约束
            modifier = Modifier.constrainAs(button) {
                // 指定约束规则
                // 使用 linkTo 指定约束，parent是ConstraintLayout的引用
                top.linkTo(parent.top, margin = 16.dp)
            }
        ) {
            Text(text = "按一下")

        }
        Text(text = "文字", modifier = Modifier.constrainAs(text) {
            top.linkTo(button.bottom, margin = 16.dp)
            centerHorizontallyTo(parent)
        })
    }
}


@Preview
@Composable
fun ConstraintTwoButton() {

    ConstraintLayout {
        var (button1, button2, text) = createRefs()

        Button(onClick = { /*TODO*/ },
            modifier = Modifier.constrainAs(button1) {
                top.linkTo(parent.top)
            }) {
            Text(text = "点一下咯")
        }

        Text(text = "我属于第一个按钮", modifier = Modifier.constrainAs(text) {
            bottom.linkTo(button1.bottom)
            centerAround(button1.end)
        })

        // 将button1和text组合起来，建立一个屏障
        val endBarrier = createEndBarrier(button1, text)

        Button(onClick = { /*TODO*/ }, modifier = Modifier.constrainAs(button2) {
            top.linkTo(parent.top)
            start.linkTo(endBarrier)
        }) {
            Text(text = "第二个按钮咯")
        }
    }


}

/**
 * 实现自动换行
 * */
@Preview
@Composable
fun AutoNextLine() {

    ConstraintLayout {
        val longText = createRef()

        val guideLine = createGuidelineFromStart(fraction = 0.5f)

        Text(text = "this is a very very very very very very very very long text",
            modifier = Modifier.constrainAs(longText) {
                linkTo(start = guideLine, end = parent.end)

                // 配置自动换行
                width = Dimension.preferredWrapContent
            })


    }

}


@Preview
@Composable
fun DecoupleConstraint() {

    BoxWithConstraints {
        val constrains = if (maxWidth < maxHeight) {
            // 竖屏
            calculateLayout(120.dp)
        } else {
            // 横屏
            calculateLayout(20.dp)
        }
        ConstraintLayout(constrains) {
            Button(onClick = { /*TODO*/ }, modifier = Modifier.layoutId("button")) {
                Text(text = "动态约束")
            }
            Text(text = "文字", modifier = Modifier.layoutId("text"))
        }
    }


}

private fun calculateLayout(margin: Dp): ConstraintSet {
    return ConstraintSet {

        val button = createRefFor("button")
        val text = createRefFor("text")

        constrain(button) {
            top.linkTo(parent.top, margin)
        }
        constrain(text) {
            top.linkTo(button.bottom, margin)
        }
    }
}
